
from rest_framework.views import Response
from utils.jwt_token import check_token
from myapp. models import UserModel

def check_login(func):
    def wrapper(self,request,*args,**kwargs):
        token = request.headers.get('Authorization')
        print('装饰器接收前端头部的token',token)

        payload = check_token(token)
        print('装饰器解码后的token',payload)

        if type(payload) == dict:
            id = payload.get('user_id')
            user = UserModel.objects.get(id=id)
            request.user = user
            return func(self,request,*args,**kwargs)
        else:
            return Response({'code':502,'msg':'未登录'})
    return wrapper







